(function($){
    $.uiBkImpl = {
        on_resize: function(e){
            e.data.overlay.css({
                width: $(window).width(),
                height: $(window).height()
            });
            $.uiBkImpl.locate($('#ui-bk-msg'), e.data.ops);
        },
        locate: function(mdiv, ops){
            var w = mdiv.width(), h = mdiv.height(), ww = $(window).width(), wh = $(window).height();
            switch (ops.align) {
                case 'tr':
                    mdiv.css({
                        top: 0,
                        right: 0
                    });
                    break;
                case 'tc':
                    mdiv.css({
                        top: 0,
                        left: (ww - w) / 2
                    });
                    break;
                case 'tl':
                    mdiv.css({
                        top: 0,
                        left: 0
                    });
                    break;
                default:
                    mdiv.css({
                        left: (ww - w) / 2,
                        top: (wh - h) / 2
                    })
                    break;
            }
        }
    };
    
    $.uiBk = function(msg, ops){
        var opts = $.extend({
            background: '#999',
            align: 'center',
            overlay: true
        }, ops), ww = $(window).width(), wh = $(window).height();
        if (opts.overlay) {
            var bk = $('#ui-bk-overlay');
            if (bk.length == 0) {
                bk = $('<div id="ui-bk-overlay"></div>');
                bk.css({
                    position: 'fixed',
                    width: ww,
                    height: wh,
                    top: 0,
                    left: 0,
                    opacity: 0,
                    background: opts.background,
                    'z-index': 998
                }).appendTo($('body'));
                if (opts.ccls) {
                    bk.addClass(opts.ccls);
                }
                bk.fadeTo(300, .8, function(){
                    if (opts.timeout) {
                        setTimeout($.uiBkc, opts.timeout);
                    }
                    if (opts.click) {
                        bk.click($.uiBkc);
                    }
                });
                $(window).bind('resize', {
                    overlay: bk,
                    ops: opts
                }, $.uiBkImpl.on_resize);
            }
        }
        var msgdiv = $('#ui-bk-msg');
        if (msgdiv.length == 0) {
            msgdiv = $('<div id="ui-bk-msg"></div>').css({
                position: 'fixed',
                margin: 0,
                padding: 0,
                'z-index': 999
            }).hide();
            msgdiv.appendTo($('body'));
        }
        msgdiv.hide().empty().html('<p id="ui-bk-msg-body">' + msg + '</p>');
        if (opts.cls) {
            msgdiv.addClass(opts.cls);
        }
        if (opts.bcls) {
            msgdiv.find('#ui-bk-msg-body"').addClass(opts.bcls);
        }
        $.uiBkImpl.locate(msgdiv, opts);
        msgdiv.fadeIn(350);
    };
    
    $.uiBkc = function(ops){
        $('#ui-bk-msg').fadeOut(300, function(){
            $('#ui-bk-msg').remove();
        });
        $('#ui-bk-overlay').fadeOut(300, function(){
            $('#ui-bk-overlay').remove();
        });
        $(window).unbind('resize', $.uiBkImpl.on_resize);
    };
    
})(jQuery);
